<?php
	App::import("Model","Config.Config");
	class BackendConfig extends Config
	{
		var $name = "BackendConfig";
		
		function save($config)
		{
			$configToSave = array("Backend"=>$config[$this->alias]);
			return parent::save($configToSave,CONFIG_FILE, false , array() ,true );
		}
		
		function initialize()
		{
		
			$timeZoneArr = Utility::getTimeZone();
			foreach($timeZoneArr as $identifierKey=>$timeZone)
			{
				$newTimeZone[$identifierKey] = $timeZone['label'];
			}
			$dateFormats = array('Y F d'=>'YYYY Month DD',
					'd F Y'=>'DD Month YYYY',
					'Y-m-d'=>'YYYY-MM-DD',
					'd-m-Y'=>'DD-MM-YYYY',
					'm-d-Y'=>'MM-DD-YYYY'
					);
			if(cr("Backend.dateFormats"))
			{
				$dateFormats = cr("Backend.dateFormats");
			}
			foreach($dateFormats as $dateFormat => $dateFormaLabel)
			{
				$dateFormaLabel = $dateFormaLabel." (".date($dateFormat).")";
				$dateFormats[$dateFormat] = $dateFormaLabel;
			}
			$this->fieldProperties = array(

				'adminUser'     => array('label'=> __('Admin User'),
							     'after' => __('Username for Administrator\'s account'),
							     'validate' => array('rule'=>VALID_NOT_EMPTY,"required"=>true)),

				'adminPassword' => array('label'=> __('Admin Password'),
								 'after' => __('Password for Administrator\'s account'),
								 'type' => 'password',
								'validate'=> array('rule'=>VALID_NOT_EMPTY,"required"=>true)),

				'siteName'      => array('label'=> __('Site Name'),
								 'after' => __('This is used in outgoing emails, enter something like "Your company billing app" or "Your Company Accounts"'),
								'validate' => array('rule'=>VALID_NOT_EMPTY,"required"=>true)),

				'email'         => array('label'=> __('Your Email'),
								 'after' => __('Important alerts will be sent to this email'),'required'=>true,
								 'validate'=>array("rule"=>"email",'message'=>__("Please enter a valid email."),'required'=>true)),

				'websiteEmail'  => array('label'=> __('Website Email'),
								 'after' => __('Any outgoing email from the system will be generated from this email. Set as noreply@sitename.com or similar.'),
								 'required'=>true,'validate'=>array("rule"=>"email",'message'=>__("Please enter a valid email."),'required'=>true)),

				'siteUrl'       => array('label'=> __('URL'),
								 'after' => __('URL where you\'re installing this software (no slash at end) with http:// in the beginning'),
								 'required'=>true,'validate'=>array("rule"=>"url",'message'=>__("Please enter a valid URL. Make sure that http:// is present in the beginning of the URL."),'required'=>true)),
				'dateFormat' => array('label'=>__("Date format"),'options'=>$dateFormats),		

				'timezone'      => array('label'   => __('Website Time Zone'),
								 'after'    => __('Select your timezone'),
								 'options' => $newTimeZone),

				'siteStatus'=>array('label'=>__('Application Status'),'options' => array('on'=>__('On'),'off'=>__('Off')),'after'=>'<br />'.__('You can use this feature to turn off application while maintenance or any such event.')),

				'siteOfflineText' => array('label'=>__("Text to be shown when site offline"),'type'=>"textarea",'after'=>__("This message will be shown if you turn off the application using the configuration above. You can add HTML content also.")),

			);
			//a workaround to disable the non required fields.
			if(cr("Backend.BackendConfig.removeFields"))
			{
				$removeFields = cr("Backend.BackendConfig.removeFields");
				$removeFields = array_combine($removeFields, $removeFields);
				$this->fieldProperties = array_diff_key($this->fieldProperties,$removeFields);
			}
			if(cr("Backend.BackendConfig.properties"))
			{
				$allowedProperty = array('label','tooltip','after','before','validate','required');
				$allowedPropertyArray = array_combine($allowedProperty, $allowedProperty);
				$fields = $this->getFields();
				foreach(cr("Backend.BackendConfig.properties") as $field=> $properties)
				{
					if(!in_array($field,$fields))
					{
						continue;
					}
					$properties = array_intersect_key($properties, $allowedPropertyArray);
					$this->setFieldProperties($field, $properties);
				}
			}
			foreach($this->fieldProperties as $field => $properties)
			{
				$this->fieldProperties[$field]['group'] = array('setup');
				$validate = array();
				if(isset($properties['required']))
				{
					$validate[] = array("rule"=>VALID_NOT_EMPTY,'required'=>true,'message'=>__("This field is required."));
				}
				if(isset($properties['validate']))
				{
					$validate = am($validate,array($properties['validate']));
				}
				if($validate)
				{
					$this->validate[$field] = $validate;
				}
			}
			parent::initialize();
		}
		
		/*
	 	* hack to validate localhost also
		 */
		function url($url)
		{
			$field = array_values($url);
			$field = $field[0];
			$return = false;
			if(strpos($field,'http://')!==false)
			{
				$return = true;
			}
			return $return;
		}
	}
?>